import { defineStore } from 'pinia'
import { showToast } from 'vant'
import { jsonp } from 'vue-jsonp'

export const useLocation = defineStore('location', {
    state: () => {
        return {
            location: '',
            city: ''
        }
    },
    actions: {
        setLocation(state: any) {
            // 逆地址解析
            jsonp(`${state.mapApi}/geocoder/v1/`, {
                location: state.location,
                key: state.mapKey,
                output: 'jsonp'
            }).then((res: any) => {
                const { status, message, result } = res
                if (status == 0) {
                    console.log(result.address_component.city)
                    this.location = state.location
                    this.city = result.address_component.city
                } else {
                    showToast(message)
                }
            })
        },
        setCity(state: any) {
            this.city = state
        }
    }
})